//======================================================================================================================
// 2007 Copyright Mystic Lake Software
//
// This file is part of The Ham Radio Network.
//
// The Ham Radio Network is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// The Ham Radio Network is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//======================================================================================================================
//
//     Author: Rudyard Merriam
//
//      $Id: NRZI.cpp 3 2007-09-19 04:25:45Z k5rud@arrl.net $
//
//======================================================================================================================
//
#include <iostream>
#include <bitset>

using namespace std;

typedef bitset<8> Bitset8;
//==========================================================================================================================
class NRZI
{
public:
    NRZI() : mBit(false) { }
    void encode(const Bitset8& in, Bitset8& out);
    void decode(const Bitset8& in, Bitset8& out);

protected:
    bool mBit;
};
//--------------------------------------------------------------------------------------------------------------------------
void NRZI::encode(const Bitset8& in, Bitset8& out)
{
    for(size_t pos = 0; pos < in.size(); pos++)
    {
        if (in[pos]) 
        {
            mBit = !mBit;
        }
        out[pos] = mBit;
    }
}
//--------------------------------------------------------------------------------------------------------------------------
void NRZI::decode(const Bitset8& in, Bitset8& out)
{
    for(size_t pos = 0; pos < in.size(); pos++)
    {
        if (in[pos] != mBit) 
        {
            mBit = !mBit;
            out[pos] = true;
        }
    }
}
//==========================================================================================================================
int main(int argc, char *argv[ ])
{
    NRZI nrzi;
    Bitset8 in;

    if (argc == 2)
    {
        if (argv[1][0] == 'E')
        {
            while (!cin.eof())
            {
                cin >> in;
                if (cin.good())
                {
                    Bitset8 out;
                    nrzi.encode(in, out);
                    cout << out << endl;
                }
            } 
        }
        else if (argv[1][0] == 'D')
        {
            while (!cin.eof())
            {
                cin >> in;
                if (cin.good())
                {
                    Bitset8 out;
                    nrzi.decode(in, out);
                    cout << out;
                }
            } 
        }
    }
    else
    {
        cerr << "(E)ncode or (D)ecode option missing" << endl;
    }
}
